package village.rainbow.slipin.adapter;

import java.util.List;
import village.rainbow.slipin.R;
import village.rainbow.slipin.datamodel.DataRow;
import village.rainbow.slipin.uicontrol.BaseArrayAdapter;
import village.rainbow.slipin.uicontrol.ResizeLayout;
import village.rainbow.slipin.util.DateUtil;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListCustomAdapter extends BaseArrayAdapter {

	private static int LAYOUT = R.layout.list_item;
	private LayoutInflater mInflater;
	
	private LruCache<String, Bitmap> mLruCache;
	
	public ListCustomAdapter(Context context, List<DataRow> objects, ResizeLayout resizeLayout) {
		super(context, LAYOUT, objects, resizeLayout);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		if(mLruCache != null)
			mLruCache.evictAll();
		
		mLruCache = new LruCache<String, Bitmap>(50);
	}
	
	@Override
	public void notifyDataSetChanged() {
		mLruCache.evictAll();
		super.notifyDataSetChanged();
	}
	
	@SuppressLint("NewApi")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder holder = null;
		
		TextView tvInsDt = null;
		TextView tvText = null;
		ImageView lbg = null;
		
		if(v == null){
			v = mInflater.inflate(LAYOUT, null);
			holder = new ViewHolder();
			tvInsDt = (TextView)v.findViewById(R.id.tv_insdt);
			tvText = (TextView)v.findViewById(R.id.tv_text);
			lbg = (ImageView)v.findViewById(R.id.layout_bg);
			holder.mTextView1 = tvInsDt;
			holder.mTextView2 = tvText;
			holder.mLayoutBase = lbg;
			v.setTag(holder);
		}else{
			holder = (ViewHolder) v.getTag();
			tvInsDt = holder.mTextView1;
			tvText = holder.mTextView2;
			lbg = holder.mLayoutBase;
		}
		
		final DataRow bean = (DataRow) getItem(position);
		
		tvText.setText(bean.getString("SCENE_TEXT", ""));
		tvText.setBackgroundColor(Color.argb(50, 0, 0, 0));
		
		String imgLocation = bean.getString("SCENE_PIC_LOCATION", "");
		
		setBackGroundImage(v, lbg, imgLocation);
		
		try{
			tvInsDt.setText(DateUtil.toPatternedDate("yyyy.MM.dd (a)hh:mm", "yyyyMMddHHmmss", bean.getString("SCENE_DATE", "")));						
		}catch(Exception e){
			e.printStackTrace();
		}
		
		tvText.setTextColor(Color.WHITE);
		tvText.setTypeface(Typeface.DEFAULT_BOLD);
		
		if(position%2==0){
			tvInsDt.setTextColor(Color.WHITE);
		}else{
			tvInsDt.setTextColor(Color.WHITE);
		}
		
		TextView tv = (TextView)v.findViewById(R.id.tv_scene_cnt);
		
		String sceneCount = bean.getString("SCENE_COUNT", "0");
		if(sceneCount.equals("0")){
			tv.setVisibility(View.GONE);
		}else{
			tv.setVisibility(View.VISIBLE);
			int cnt = Integer.parseInt(sceneCount);
			tv.setText("x" + (cnt+1));
		}
		
		View vGps = (View)v.findViewById(R.id.v_gps);
		View vClock = (View)v.findViewById(R.id.v_clock);
		
		vClock.setVisibility(bean.getString("ALARM_DATE", "").equals("") ? View.GONE : View.VISIBLE);
		vGps.setVisibility(bean.getString("LOCATION_USE_YN", "").equals("Y") ? View.VISIBLE : View.GONE);
		
		return v;
	}
	
	private void setBackGroundImage(final View v, final ImageView lbg, final String imgLocation) {
		Handler h = new Handler();
		h.post(new Runnable(){
			@Override
			public void run() {
				
				if(mLruCache.get(imgLocation) == null){
					Drawable d = BitmapDrawable.createFromPath(imgLocation);
					if(d == null)
						return;
					
					Bitmap bitmap = cropDrawable(d, lbg);
					lbg.setImageBitmap(bitmap);
					RelativeLayout layoutAlpha = (RelativeLayout)v.findViewById(R.id.layout_alpha);
					layoutAlpha.setBackgroundColor(Color.argb(50, 0, 0, 0));
					
					mLruCache.put(imgLocation, bitmap);
					
				}else{
					
					Bitmap bitmap = mLruCache.get(imgLocation);
					lbg.setImageBitmap(bitmap);
					RelativeLayout layoutAlpha = (RelativeLayout)v.findViewById(R.id.layout_alpha);
					layoutAlpha.setBackgroundColor(Color.argb(50, 0, 0, 0));
					
				}
				
			}
		});
	}

	private Bitmap cropDrawable(Drawable d, ImageView lb) {
		int width = lb.getWidth();
		int height = lb.getHeight();
		if(width <= 0 || height <= 0)
			return null;
		
		Bitmap bitmap = convertToBitmap(d, width, height);
		return bitmap;
	}
	
	public Bitmap convertToBitmap(Drawable drawable, int widthPixels, int heightPixels) {
		
		Bitmap baseBitmap = ((BitmapDrawable)drawable).getBitmap();
		Bitmap mutBit = null;
		if(widthPixels > baseBitmap.getWidth()){
			double percent = (double)widthPixels/(double)baseBitmap.getWidth();
			mutBit = Bitmap.createScaledBitmap(baseBitmap, (int)(baseBitmap.getWidth()*percent), (int)(baseBitmap.getHeight()*percent), true);
			int yOffset = (mutBit.getHeight() - heightPixels)/2;
			mutBit = Bitmap.createBitmap(mutBit, 0, yOffset, mutBit.getWidth(), heightPixels);
		}else{
			
			heightPixels = heightPixels < baseBitmap.getHeight() ? heightPixels : baseBitmap.getHeight();
			mutBit = Bitmap.createBitmap(baseBitmap, 0, 0, widthPixels, heightPixels);// Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
		}
		
		// 테두리를 둥글게 변경해야 하는데...
	    baseBitmap.recycle();
	    return mutBit;
	}

//	public DataTable getCheckedItems(){
//		DataTable chkedList = new DataTable();
//		for(int i=0; i<this.getCount(); i++){
//			DataRow row = this.getItem(i);
//			boolean chk = (Boolean)row.getTag("CHK", false);
//			if(!chk)
//				continue;
//			chkedList.add(row);
//		}
//		return chkedList;
//	}
	
	class ViewHolder{
		public TextView mTextView1;
		public TextView mTextView2;
		public ImageView mLayoutBase;
	}
	
}
